package com.duing.service;

import com.duing.entity.Fun;
import com.duing.mapper.FunMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class FunService {

    @Autowired
    private FunMapper mapper;

   public List<Fun> selectAll(){
     return mapper.selectAll();
    }

    public Fun selectFid(Integer fid){
     return mapper.selectFid(fid);
    }


    public List<Fun> selectHierarchy(){
        List<Fun> funBos =new ArrayList<>();

        List<Fun> fun = mapper.selectAll();
        for (Fun fun1 : fun) {
            if (fun1.getPid()==-1){
                List<Fun> hierarchy = Hierarchy(fun1.getFid(), fun);//子类的属性

                fun1.setFunList(hierarchy);
                funBos.add(fun1);
            }
        }
        return funBos;
    }

    private List<Fun> Hierarchy(Integer fid,List<Fun> fun){
        List<Fun> funBos =new ArrayList<>();
        for (Fun fun1 : fun) {
            if (fun1.getPid()==fid){
             funBos.add(fun1);
            }
        }

        return funBos;
    }

}
